package com.zzb.framework.frameworkcontroller.log;

import com.zzb.framework.common.BasePage;
import com.zzb.framework.common.BaseResponse;
import com.zzb.framework.common.ServiceException;
import com.zzb.framework.frameworkcontroller.common.FrameworkBaseController;
import com.zzb.framework.log.entity.SysLog;
import com.zzb.framework.log.form.SysLogSearchForm;
import com.zzb.framework.log.service.SysLogService;
import com.zzb.framework.role.entity.IUser;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
* 系统日志controller
*/
@Controller
@RequestMapping("/framework/sysLog")
public class FrameworkSysLogController extends FrameworkBaseController {
    private static final Logger logger = LoggerFactory.getLogger(FrameworkSysLogController.class);
    @Autowired
    private SysLogService sysLogService;

    /**系统日志管理首页*/
    @RequestMapping({"","/","/index"})
    public String index(){
        return "framework/log/sysLog/index";
    }

    /**系统日志列表json*/
    @RequestMapping("list")
    @ResponseBody
    public BasePage<SysLog> list(SysLogSearchForm sysLogSearchForm){
        return sysLogService.queryPage(sysLogSearchForm);
    }

    /**系统日志表单页面*/
    @RequestMapping("form")
    public String form(String id, Model model){
        SysLog sysLog;
        if(StringUtils.isEmpty(id)){
            sysLog = new SysLog();
        } else{
            sysLog = sysLogService.get(id);
            if(sysLog == null) sysLog = new SysLog();
        }
        model.addAttribute("sysLog",sysLog);
        return "framework/log/sysLog/form";
    }

    /**系统日志保存*/
    @RequestMapping("/save")
    @ResponseBody
    public BaseResponse<SysLog> save(SysLog sysLog , HttpServletRequest request){
        try {
            IUser user = getCurrentUser(request);
            return sysLogService.saveSysLog(sysLog, user);
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse<>(BaseResponse.FAIL,e.getMessage());
        }
    }

    /**系统日志删除*/
    @RequestMapping("/delete")
    @ResponseBody
    public BaseResponse delete(String id,HttpServletRequest request){
        try {
            return sysLogService.deleteSysLog(id,getCurrentUser(request));
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse(BaseResponse.FAIL,e.getMessage());
        }
    }

    /**系统日志开启*/
    @RequestMapping("/open")
    @ResponseBody
    public BaseResponse open(String id ,HttpServletRequest request){
        try {
            return sysLogService.recoveryById(id,getCurrentUser(request));
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse(BaseResponse.FAIL,e.getMessage());
        }
    }

    /**系统日志屏蔽*/
    @RequestMapping("/deny")
    @ResponseBody
    public BaseResponse deny(String id ,HttpServletRequest request){
        try {
            return sysLogService.trashById(id,getCurrentUser(request));
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse(BaseResponse.FAIL,e.getMessage());
        }
    }

}
